<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:"hr-xz-tsalaryAdjust-user-cache",
				isTrue:false,
				changeTextArea:false
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			getWorkParam2(){
				let _that = this
				return{
					userId:_that.$refs.autoform.formdata.infonameId?_that.$refs.autoform.formdata.infonameId:''
				}
			},
			getSalaryList(id){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getSalaryList1",
					{
						userId: id,
						posId: _that.$refs.autoform.formdata.salaryPosId,
						busCode: _that.busCode
					}
				).then(res=>{
					for(let item of res.value){
						if(item.salaryName != 'salary1' && item.salaryName != 'salary2' && item.salaryName != 'salary3' && item.salaryName != 'salary4'){
							item.showSubTitle2 = false
						}
					}
					_that.subEditorObj["title_salary"].workList = res.value
				})
			},
			getEmpBasicInfo(id){
				let _that = this
				_that.om.request(
					"temployee/getEmpBasicInfo",
					{
						userId: id
					}
				).then(res=>{
					if(res.value){
						let formdata = _that.$refs.autoform.formdata
						let d = res.value  
						formdata.sexName = d.sexName
						formdata.birthdate = d.birthdate
						formdata.birthPlace = d.birthPlace
						formdata.entrydate = d.entrydate
						formdata.areaName = d.areaName
						formdata.idTypeName = d.idTypeName
						formdata.idcard = d.idcard
						formdata.mobile = d.mobile
						formdata.highestEduName = d.highestEduName
						formdata.certificateValidDate = d.certificateValidDate
						formdata.nationName = d.nationName
						formdata.marriageName = d.marriageName
						formdata.politicalName = d.politicalName
						formdata.bloodTypeName = d.bloodTypeName
						formdata.userno = d.userno
						formdata.registerCategoryName = d.registerCategoryName
						formdata.birthaddress = d.birthaddress
						formdata.currentaddress = d.currentaddress
					}
				})
			},
			verifyUser(d){
				let _that = this
				_that.om.request(
					"tsalaryAdjust/verifyUser",
					{
						userId:d.id,
						zposId:_that.$refs.autoform.formdata.zposId
					},
					"post"
				).then(res=>{
					if(res.value){
						errorModal(_that,"提示信息",ignoreHtml(res.value),function(){
							_that.$refs.autoform.clear("userInfo","infoname")
						})
					}else{
						_that.$refs.autoform.formdata.infonameId = d.id
						_that.$refs.autoform.formdata.userName = d.username
						_that.getInfoList(d.id)
						_that.getPostList(d.id)
						_that.getEmpBasicInfo(d.id)
					}
				})
			},
			inputSalary1(){
				let _that = this
				_that.$refs.autoform.formdata.monthSalary = Number(_that.$refs.autoform.formdata.salary1) + 
				Number(_that.$refs.autoform.formdata.salary2) +
				Number(_that.$refs.autoform.formdata.salary3)
				_that.$refs.autoform.formdata.yearSalary =  _that.$refs.autoform.formdata.monthSalary * 12
			},
			
			formSubmit(){
				let _that = this
				if(_that.subEditorObj['title_salary']){
					_that.formdata.salaryStr = JSON.stringify(_that.subEditorObj['title_salary'].workList)
				}
				if(!_that.checkedit){
					_that.om.formSave("tsalaryAdjust/addTsalaryAdjust",_that.formdata,_that)
				}else{
					_that.om.formSave("tsalaryAdjust/updateData",_that.formdata,_that)
				}
			},
			verifyUserName(){
				let _that = this
				if(!_that.$refs.autoform.formdata.zposId){
					_that.om.toast("请先选择制表人岗位")
					return false
				}else{
					return true
				}
			},
			onSelectZpos(item){
				let _that = this
				_that.$refs.autoform.changeEditor("userInfo","infonameId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							zposId:item.id
						}),
						method:"post",
						url:"tsalaryAdjust/getEmpByZpos",
						initload:true,
						namefield:"username",
						cachename:_that.userCacheKey,
						placeholder: "请输入姓名"
					}
				})
			},
			getInfoList(id){
				let _that = this 
				_that.om.request(
					"tworkInfo/getInfo1",
					{
						userId:id
					},
					"post"
				).then(res=>{
					_that.subEditorObj["workInfo"].workList = res.value
				})
			},
			getSalaryInfo(id){
				let _that = this
				_that.om.request(
					"tsalaryAdjust/getSalaryInfo",
					{
						userId:id
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.formdata.salaryPos_ = res.value.salaryPos_
					_that.$refs.autoform.formdata.effectDate_ = res.value.effectDate_
					_that.$refs.autoform.formdata.salaryWay_ = res.value.salaryWay_
					_that.$refs.autoform.formdata.posGrade_ = res.value.posGrade_
					_that.$refs.autoform.formdata.salaryLevel_ = res.value.salaryLevel_
					_that.$refs.autoform.formdata.levelSalaryRange_ = res.value.levelSalaryRange_
					_that.$refs.autoform.formdata.salaryEffectDate_ = res.value.salaryEffectDate_
					_that.$refs.autoform.formdata.monthSalary_ = res.value.monthSalary_
					_that.$refs.autoform.formdata.yearSalary_ = res.value.yearSalary_
				})
			},
			getSalaryInfo1(id){
				let _that = this
				_that.om.request(
					"temployee/getSalaryInfo",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.salary1_ = res.value.salary1
					_that.$refs.autoform.formdata.salary2_ = res.value.salary2
					_that.$refs.autoform.formdata.salary3_ = res.value.salary3
				})
			},
			
			getPostList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosList",
					{
						"user.id":id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title9","salaryPosId","data",configSelectData(res,"id","posName"))
					_that.$refs.autoform.initSelectIndex("title9","salaryPosId")
				})
			
			},
			getSalaryInfo2(id){
				let _that = this
				_that.om.request(
					"temployee/getSalaryInfo",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.monthSalary = res.value.monthSalaryStandard
					_that.$refs.autoform.formdata.yearSalary = res.value.yearSalary
				})
			},
			onSelectSalaryPos(item){
				let _that = this
				_that.$refs.autoform.formdata.salaryInstId = item.instId
				_that.$refs.autoform.formdata.salaryInstName = item.instName
				_that.$refs.autoform.formdata.salaryWay1 = item.salarycatName
				_that.$refs.autoform.formdata.posGrade = item.careercatName
				_that.getSalaryList(_that.$refs.autoform.formdata.infonameId)
				_that.om.request(
					"tinstitution/getInstInfo",
					{
						instId:item.instId
					}
				).then(res=>{
					_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
					_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
				})
				_that.$refs.autoform.formdata.attributeId = item.attributeId
				_that.$refs.autoform.formdata.attributeName = item.attributeName
				_that.$refs.autoform.formdata.posGradeId = item.careercatId
				_that.$refs.autoform.formdata.posGradeName = item.careercatName
			},
			onSelectSalaryLevel(item){
				let _that = this
				_that.om.request(
					"salaryStandard/getSalaryRange1",
					{
						posId: _that.$refs.autoform.formdata.salaryPosId,
						levelId: item.id
					},
					"post"
				).then(res=>{
					if(!res.value.str){
						_that.$refs.autoform.formdata.levelSalaryRange = ''
					}else{
						_that.$refs.autoform.formdata.levelSalaryRange = res.value.str
					}
					
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId 
				if(fileId){
					ajaxArr.push({
						url:"tsalaryAdjust/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					if(_that.subEditorObj["workInfo"]){
						changeEditor("workInfo","work_info",{
							showAdd:false,
							showDel:false,
							titleTemplate: "岗位名称:<%=posName%>|任职方式:<%=workWayName%>",
							subtitle1Template: "机构简称:<%=instShort%>|组织简称:<%=orgShort%>",
							subtitle2Template: "岗位职级:<%=posCareercat %>|计酬方式:<%=salaryWay%>",
							subtitle3Template: "上岗日期:<%=workDate%>"
						},null,mainData)
					}
					if(_that.subEditorObj["title_salary"]){
						let att = {
							showAdd:false,
							showDel:false,
							getWorkParam:'getWorkParam2'
						}
						let configData = _that.subConfigData["title_salary"]
						if(hasEditor("title_salary","itemName",configData)){
							att.titleTemplate = "<%= itemName || '未填写'%>"
						}
						if(hasEditor("title_salary","beforeAdjustId",configData)){
							let title = getEditorTitle("title_salary","beforeAdjustId",configData)
							att.subtitle1Template = title + ":<%= beforeAdjust || '无'%>"
						}
						if(hasEditor("title_salary","afterAdjustId",configData)){
							let title = getEditorTitle("title_salary","afterAdjustId",configData)
							if(att.subtitle1Template){
								att.subtitle1Template = att.subtitle1Template +' | '+title + ":<%= afterAdjust || '未填写'%>"
							}else{
								att.subtitle1Template =title + ":<%= afterAdjust || '未填写'%>"
							}
						}
						if(hasEditor("title_salary","salary",configData)){
							let title = getEditorTitle("title_salary","salary",configData)
							att.subtitle2Template = title + ":<%= salary || '未填写'%>"
						}
						if(hasEditor("title_salary","remark",configData)){
							let title = getEditorTitle("title_salary","remark",configData)
							att.subtitle3Template = title + ":<%= remark || '未填写'%>"
						}
						changeEditor("title_salary","salaryStr",att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value.temployeeResponse){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.temployeeResponse[index])
								_that.$set(_that.formdata,index,res[1].value.temployeeResponse[index])
							}
						}
						for(let index in res[1].value.tsalaryAdjust){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tsalaryAdjust[index])
								_that.$set(_that.formdata,index,res[1].value.tsalaryAdjust[index])
							}
						}
						
						if(_that.subEditorObj["title_salary"]){
							_that.subEditorObj["title_salary"].workList = res[1].value.salaryList
							let dataList = _that._.cloneDeep(res[1].value.salaryList)
							for(let item of dataList){
								if(item.salaryName != 'salary1' && item.salaryName != 'salary2' && item.salaryName != 'salary3' && item.salaryName != 'salary4'){
									item.showSubTitle2 = false
								}
							}
							_that.subEditorObj["title_salary"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr3",JSON.stringify(dataList))
							_that.$set(_that.formdata,"objStr3",JSON.stringify(dataList))
							let configData = _that.subConfigData["title_salary"]
							let editor = getEditor("title_salary","afterAdjustId",configData)
							if(editor.checkedit){
								for(let salaryItem of _that.subEditorObj["title_salary"].workList){
									if(salaryItem.afterAdjustId){
										return
									}
								}
								_that.getSalaryList(_that.$refs.autoform.formdata.infonameId)
							}
						}
						let dataList = _that._.cloneDeep(res[1].value.tworkInfos)
						_that.subEditorObj["workInfo"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
						delete res[1].value.dataList
						_that.$nextTick(function(){
							let obj = {
								id:_that.formdata.zposId
							}
							_that.onSelectZpos(obj)
							_that.getPostList(_that.formdata.infonameId)
							_that.$refs.autoform.initSelectIndex("title9","salaryPosId")
						})
						
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zpsoId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							editor.workList = res.data
							if(fieldId =="title_salary"){ 
								let dataList = JSON.parse(JSON.stringify(_that.subEditorObj["title_salary"].workList))
									let num = ""
									for(let item of dataList){
										if(item.salaryName == "salary1"){
											num = Number(item.afterAdjust)
										}else if(item.salaryName =="salary2"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary3"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary4"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary5"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary6"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary7"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary8"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary9"){
											item.afterAdjust = num
										}
									}
									_that.subEditorObj['title_salary'].workList = dataList
								}
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.verifyUser(d)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>